<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\MaterialDelivery;
use App\Services\MaterialDeliveryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MaterialDeliveryController extends Controller
{
    protected $MaterialDeliveryService;
    public function __construct(MaterialDeliveryService $MaterialDeliveryService)
    {
        $this->MaterialDeliveryService = $MaterialDeliveryService;
    }

    public function index(Request $Request) {
        if(!$Request->material_id){
            return response()->json(['code' => '404', 'msg' => '请选择材料']);
        }
        $data = MaterialDelivery::where('material_id',$Request->material_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $Request)
    {
        $input['user_id'] = Auth::user()->id;
        $input['data'] = json_decode($Request->data);
        $input['material_id'] = $Request->material_id;
        $input['project_id'] = $Request->project_id;
        $data = $this->MaterialDeliveryService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->material_id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $input['user_id'] = Auth::user()->id;
        $input['data'] = json_decode($Request->data);
        $input['material_id'] = $Request->material_id;
        $input['project_id'] = $Request->project_id;
        $data = $this->MaterialDeliveryService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = MaterialDelivery::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
